package com.subway.babynote.db;

import java.util.Collection;
import java.util.Iterator;

import com.subway.babynote.db.TableInfoList.TableInfo;
import com.subway.babynote.syncdata.tableName;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class create_localdb {
	SQLiteDatabase db = null;
	public create_localdb(SQLiteDatabase _db)
	{
		this.db = _db;
	}
	private boolean checkexists()
	{
		Cursor cs = db.rawQuery(TableInfoList.instance.getsqls(tableName.t_sqlite_master).selectsql, null);
		if(cs.getCount() > 0)
			return true;
		else
			return false;
	}
	
	public int createdb()
	{
		int iret = -1;
		if(this.checkexists())
		{
			logging.info("createdb", "db exists, ignore");
			iret = 0;
			return iret;
		}
		Collection<TableInfo> tl = TableInfoList.instance.m.values();
		Iterator<TableInfo> it = tl.iterator();
		try
		{
			while(it.hasNext())
			{
				TableInfo ti = it.next();
				db.execSQL(ti.createsql);
			}
			iret = 0;
		}
		catch(Exception ex)
		{
			logging.err("createdb", ex.getMessage());
			iret = -1;
		}
		
		return iret;
	}

}
